import { Component } from "react";
import PropTypes from "prop-types";


export default class Votemain extends Component{
    static contextTypes={//后代组件，接收参数(参数的格式，必须与祖先一致)
         supNum:PropTypes.number,
         oppNum:PropTypes.number
    }
    render(){
        //接收后，参数就在this.context里面，接收几个，里面就有几个参数
        //console.log(this.context);
        let {supNum,oppNum}=this.context;
        return <div>
            <h2>支持人数：{supNum}</h2>
            <h2>反对人数：{oppNum}</h2>
            <h2>支持率：{this.computed()}</h2>
        </div>
    }
    computed=()=>{
      let {supNum,oppNum}=this.context;
      let total=supNum+oppNum;
      if(total<=0){
         return "0.00%";
      }
      return (supNum/total*100).toFixed(2)+"%";
    }
}